<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userId">
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.userId" placeholder="请选择医生职位">
          <el-option
            v-for="item in users"
            :label="item.name"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="departmentId">
<!--        <el-input-->
<!--          v-model="queryParams.departmentId"-->
<!--          placeholder="请输入科室id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-cascader
          v-model="treeIds"
          :options="depts"
          :props="areaTree"
          @change="searchClick"></el-cascader>
      </el-form-item>
      <el-form-item label="医生" prop="docId">
<!--        <el-input-->
<!--          v-model="queryParams.docId"-->
<!--          placeholder="请输入医生id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.docId" placeholder="请选择医生职位">
          <el-option
            v-for="item in dockers"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="患者" prop="patId">
<!--        <el-input-->
<!--          v-model="queryParams.patId"-->
<!--          placeholder="请输入患者编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.patId" placeholder="请选择医生职位">
          <el-option
            v-for="item in patients"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号类型" prop="typeId">
<!--        <el-input-->
<!--          v-model="queryParams.typeId"-->
<!--          placeholder="请输入挂号类型id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.typeId" placeholder="请选择医生职位">
          <el-option
            v-for="item in types"
            :label="item.typeName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="费用" prop="money">-->
<!--        <el-input-->
<!--          v-model="queryParams.money"-->
<!--          placeholder="请输入费用"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="预约日期" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="时间段id" prop="timeId">
<!--        <el-input-->
<!--          v-model="queryParams.timeId"-->
<!--          placeholder="请输入时间段id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.timeId" placeholder="请选择医生职位">
          <el-option
            v-for="item in times"
            :label="item.startTime+'-'+item.endTime"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预约状态" prop="state">
<!--        <el-input-->
<!--          v-model="queryParams.state"-->
<!--          placeholder="请输入预约状态，0等待，1完成，2取消"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.state" placeholder="请选择删除状态">
          <el-option label="等待" value="0"></el-option>
          <el-option label="完成" value="1"></el-option>
          <el-option label="取消" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="删除状态" prop="del">
<!--        <el-input-->
<!--          v-model="queryParams.del"-->
<!--          placeholder="请输入删除状态，0未，1删除"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.del" placeholder="请选择删除状态">
          <el-option label="未删除" value="0"></el-option>
          <el-option label="已删除" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="true"
          @click="handleAdd"
          v-hasPermi="['system:convention:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="true"
          @click="handleUpdate"
          v-hasPermi="['system:convention:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:convention:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:convention:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="conventionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预约编号" align="center" prop="id" />
      <el-table-column label="用户姓名" align="center" prop="user.name" />
      <el-table-column label="科室" align="center" prop="deName" />
      <el-table-column label="医生" align="center" prop="doName" />
      <el-table-column label="患者姓名" align="center" prop="patient.name" />
      <el-table-column label="挂号类型" align="center" prop="reName" />
      <el-table-column label="费用" align="center" prop="prMoney" />
      <el-table-column label="预约日期" align="center" prop="date" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="时间段" align="center" prop="timeId">
        <template slot-scope="scope">
          <span>{{scope.row.startTime}}-{{scope.row.endTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约状态" align="center" prop="state" >
        <template slot-scope="scop">
          <span v-if="scop.row.state == 0">等待</span>
          <span v-if="scop.row.state == 1">完成</span>
          <span v-if="scop.row.state == 2">取消</span>
        </template>
      </el-table-column>
      <el-table-column label="删除状态" align="center" prop="del">
        <template slot-scope="scop">
          <span v-if="scop.row.del == 0">未删除</span>
          <span v-if="scop.row.del == 1">已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:convention:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:convention:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改挂号预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="科室id" prop="departmentId">
          <el-input v-model="form.departmentId" placeholder="请输入科室id" />
        </el-form-item>
        <el-form-item label="医生id" prop="docId">
          <el-input v-model="form.docId" placeholder="请输入医生id" />
        </el-form-item>
        <el-form-item label="患者编号" prop="patId">
          <el-input v-model="form.patId" placeholder="请输入患者编号" />
        </el-form-item>
        <el-form-item label="挂号类型id" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入挂号类型id" />
        </el-form-item>
        <el-form-item label="费用" prop="money">
          <el-input v-model="form.money" placeholder="请输入费用" />
        </el-form-item>
        <el-form-item label="预约日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预约日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时间段id" prop="timeId">
          <el-input v-model="form.timeId" placeholder="请输入时间段id" />
        </el-form-item>
        <el-form-item label="预约状态，0等待，1完成，2取消" prop="state">
          <el-input v-model="form.state" placeholder="请输入预约状态，0等待，1完成，2取消" />
        </el-form-item>
        <el-form-item label="删除状态，0未，1删除" prop="del">
          <el-input v-model="form.del" placeholder="请输入删除状态，0未，1删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConvention, getConvention, delConvention, addConvention, updateConvention,findUsers,findDockers,findPatients,findTypes,findTimes,selectDeptTrees } from "@/api/system/convention";

export default {
  name: "Convention",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号预约表格数据
      conventionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        departmentId: null,
        docId: null,
        patId: null,
        typeId: null,
        money: null,
        date: null,
        timeId: null,
        state: null,
        del: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      users:[], //用户容器
      dockers:[], //医生容器
      depts:[], //科室容器
      types:[], //挂号类型容器
      times:[], //时间段容器
      patients:[], //患者容器
      //级联规则
      areaTree:{
        label:"name",
        value:"id",
        children:"department",
        expandTrigger: "hover",
      },
      treeIds:[], //级联选择装id的容器

    };
  },
  created() {
    this.getList();
    this.findUsers();
    this.findDepts();
    this.findDockers();
    this.findTypes();
    this.findPatients();
    this.findTimes();
  },
  methods: {
    /** 查询挂号预约列表 */
    getList() {
      this.loading = true;
      listConvention(this.queryParams).then(response => {
        console.log("预约挂号：",response);
        this.conventionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        departmentId: null,
        docId: null,
        patId: null,
        typeId: null,
        money: null,
        date: null,
        timeId: null,
        state: null,
        createTime: null,
        updateTime: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.treeIds=[];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加挂号预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConvention(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改挂号预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConvention(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConvention(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除挂号预约编号为"' + ids + '"的数据项？').then(function() {
        return delConvention(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/convention/export', {
        ...this.queryParams
      }, `convention_${new Date().getTime()}.xlsx`)
    },
    //科室搜索赋值
    searchClick(){
      this.queryParams.departmentId = this.treeIds[this.treeIds.length-1];
    },
    //查询全部用户
    findUsers(){
      findUsers().then(res=>{
        console.log("用户：",res);
        this.users = res.rows;
      })
    },
    //查询全部科室
    findDepts(){
      selectDeptTrees().then(res=>{
        console.log("科室：",res)
        this.depts=res.data;
      })
    },
    //查询全部医生
    findDockers(){
      findDockers().then(res=>{
        console.log("医生：",res);
        this.dockers=res.rows;
      })
    },
    //查询全部挂号类型
    findTypes(){
      findTypes().then(res=>{
        console.log("类型：",res);
        this.types=res.rows;
      })
    },
    //查询全部患者
    findPatients(){
      findPatients().then(res=>{
        console.log("患者：",res);
        this.patients=res.rows;
      })
    },
    //查询全部患者
    findTimes(){
      findTimes().then(res=>{
        console.log("时间：",res);
        this.times=res.rows;
      })
    },


  }
};
</script>
